﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CCSService_WebRole
{
    public partial class TestPaymentServer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Grab the invoiceID GUID that is sent from the client
            string invoiceID = Request.QueryString["invoice"];
            string developerID = Request.QueryString["custom"];

            if (invoiceID == null)
            {
                Response.Write("Your application needs to provide an invoiceID " +
                    "in the QueryString!");

                lblDevID.Visible = false;
                lblInvoiceID.Visible = false;
                lblMessage.Visible = false;
                lblCustEmail.Visible = false;
                lblCustFirstName.Visible = false;
                lblReceiverEmail.Visible = false;

                txtDevID.Visible = false;
                txtInvoiceID.Visible = false;
                txtDevID.Visible = false;
                txtCustEmail.Visible = false;
                txtCustFirstName.Visible = false;
                txtReceiverEmail.Visible = false;

                btnPay.Visible = false;

            }
            else
            {
                txtInvoiceID.Text = invoiceID;
                txtReceiverEmail.Text = Request.QueryString["business"];
                txtDevID.Text = developerID;
            }
        }

        protected void btnPay_Click(object sender, EventArgs e)
        {
            // Create the IpnTransaction
            IpnTransaction ipn = new IpnTransaction();

            ipn.RawIPN = Request.Form.ToString();
            ipn.Invoice = txtInvoiceID.Text;
            ipn.ReceiverEmail = txtReceiverEmail.Text;
            ipn.McCurrency = PayPal.Web.Controls.CurrencyCode.USD;
            ipn.PaymentType = PaymentType.Instant;
            ipn.PaymentStatus = PaymentStatus.Completed;
            ipn.McFee = float.Parse(Request.QueryString["amount"]);
            ipn.McGross = float.Parse(Request.QueryString["amount"]);
            ipn.Business = Request.QueryString["business"];
            ipn.Custom = Request.QueryString["custom"];

            IpnCustomerInfo ci = new IpnCustomerInfo();
            ci.Email = txtCustEmail.Text;
            ci.FirstName = txtCustFirstName.Text;

            ipn.CustomerInfo = ci;

            // Make the request to the web service
            IpnService service = new IpnService();
            service.ProcessTransaction(ipn);
            lblMessage.Text = "Test payment successfully submitted.  Please return to the application to " +
                "check the activation status.";
        }
    }
}
